package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.form.FormRequest;
import cc.linker.steplink.crm.commons.model.FormLoginDto;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.FormFilter;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.domain.LabelPo;
import cc.linker.steplink.crm.merchanth5.service.acs.DynamicService;
import cc.linker.steplink.crm.service.ILabelNewService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/labelNew")
public class LabelNewController {

    @Autowired
    private ILabelNewService labelNewService;
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private FormRequest formRequest;
    @Autowired
    private FormFilter formFilter;

    /*
     * 标签列表
     */
    @GetMapping("/list")
    public Object list(
            HttpServletRequest request,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        PageHelper.startPage(pageIndex, pageSize);
        Map<String, Object> labelMap = new HashMap<>();
        String orgId = userinfo.getOrgId();
        labelMap.put("orgId", orgId);
        return new PageInfo<>(labelNewService.listLabel(labelMap));
    }

    /*
     * 标签列表
     */
    @GetMapping("/allList")
    public Object allList(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> labelMap = new HashMap<>();
        String orgId = userinfo.getOrgId();
        labelMap.put("orgId", orgId);
        return labelNewService.listLabel(labelMap);
    }

    /**
     * 单个标签详情
     *
     * @return
     */
    @GetMapping("/get")
    public Object get(String labelId){
        return labelNewService.getLabel(labelId);
    }


    /**
     * 添加标签
     *
     */
    @PostMapping("/add")
    public void add(HttpServletRequest request, @RequestBody Map<String, Object> label) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        LabelPo record = new LabelPo();
        String uuid = UUID.randomUUID().toString();
        record.setLabelId(uuid);
        String labelName = (String) label.get("labelName");
        record.setLabelName(labelName);
        String orgId = userinfo.getOrgId();
        record.setOrgId(orgId);
        labelNewService.addLabel(record);
        String adminId = userinfo.getAdminId();
        dynamicService.recording(adminId, orgId, 5,"新增"+labelName);
    }

    /**
     * 更新标签
     */
    @PostMapping("/update")
    public void update(HttpServletRequest request, @RequestBody Map<String, Object> label){
        String labelId = (String) label.get("labelId");
        String labelName = (String) label.get("labelName");
        LabelPo record = new LabelPo();
        record.setLabelId(labelId);
        record.setLabelName(labelName);
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        record.setOrgId(userinfo.getOrgId());
        labelNewService.updateLabel(record);
    }


    /**
     * 删除标签
     */
    @PostMapping("/delete")
    public void del(@RequestBody Map<String, Object> label, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String labelId = (String) label.get("labelId");
        LabelPo label1 = labelNewService.getLabel(labelId);
        labelNewService.delLabel(labelId);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        dynamicService.recording(adminId, orgId, 6,"删除"+label1.getLabelName());
    }


    /**
     * 策略列表
     */
    @GetMapping("/tactics/list")
    public Object tacticsList(
            HttpServletRequest request,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize)
            {
                PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        PageHelper.startPage(pageIndex, pageSize);
        Map<String, Object> tacticsMap = new HashMap<>();
        String orgId = userinfo.getOrgId();
        tacticsMap.put("orgId", orgId);
        return new PageInfo<>(labelNewService.tacticsList(tacticsMap));
    }


    /**
     * 策略详情
     *
     * @return
     */
    @GetMapping("/tactics/get")
    public Object getTactics(String tacticsId){
        return labelNewService.getTactics(tacticsId);
    }

    /**
     * 获取报名列表（包涵单多选选项）
     */
    @GetMapping("/tactics/enrollAttrList")
    public List<Map> enrollAttrList(HttpServletRequest request,String enrollId){
        return labelNewService.enrollAttrList(enrollId);
    }

    /**
     * 获取表单列表（包涵单多选选项）
     */
    @GetMapping("/tactics/formAttrList")
    public List<Map> formAttrList(HttpServletRequest request,String formId){
        FormLoginDto formLoginDto = formFilter.getFormLoginDto(request, false);
        Map params = new HashMap();
        params.put("formId",formId);
        Map<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> map1 =   (Map<String, Object>)  formRequest.doRequest("/pc/form/templates/getByFormId", params, formLoginDto);
            if(map1!=null && map1.size()>0){
                map = map1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return labelNewService.formAttrList(map);
    }


    /**
     * 添加策略
     */
    @PostMapping("/tactics/add")
    public void addTactics(HttpServletRequest request, @RequestBody Map<String, Object> tactics){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        tactics.put("orgId",orgId);
        labelNewService.addTactics(tactics);
    }


   /**
     * 编辑策略
     */
    @PostMapping("/tactics/update")
    public void updateTactics(@RequestBody Map<String, Object> tactics){
        labelNewService.updateTactics(tactics);
    }



    /**
     * 删除策略
     */
    @PostMapping("/tactics/delete")
    public void delTactics(@RequestBody Map<String, Object> tactics){
        Long tacticsId = Long.valueOf(tactics.get("tacticsId").toString());
        labelNewService.delTactics(tacticsId);
    }


    /**
     * 更改状态策略
     */
    @PostMapping("/tactics/updateStatus")
    public void updateStatus(@RequestBody Map<String, Object> tactics){
        labelNewService.updateStatus(tactics);
    }


}
